home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / stcron4.lzh / STCRON4 / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-08  |  2.5 KB  |  113 lines

  1. /* CROND & CRONTAB: (c) Kees Lemmens, Netherlands; June 1993.
  2.  
  3.     Programs for ATARI ST (running under MINT) to make it possible
  4.     to run background jobs at regular intervals.
  5.   
  6.     Any questions or suggestions about this program can be send to:
  7.     lemmens@dv.twi.tudelft.nl
  8. */
  9.  
  10. #include "cron.h"
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <signal.h>
  17. #include <errno.h>
  18. #include <stdarg.h>
  19.  
  20. char *ux2dos(char *string)    /* convert / to \ */
  21. {    char *tmp;
  22.     while((tmp=strchr(string,'/')) != NULL)
  23.         *tmp='\\';
  24.     return string;
  25. }
  26.  
  27. void LogMsg(char *name,int pid,char *fmt,...)
  28. {
  29.   /* If LOGFILE is defined this function writes to the */
  30.   /* appropriate file; otherwise, it does nothing. */
  31.  
  32. #if defined(LOGFILE) || defined(DEBUG)
  33.     va_list ptr;
  34.     char *msg;
  35.     long now = time((long *) 0);
  36.     char *ti = ctime(&now);
  37.     FILE *log_fd;
  38.  
  39.     msg = malloc(strlen(name) + strlen(fmt) + MAX_TEMPSTR);
  40.     va_start(ptr,fmt);
  41.  
  42.     sprintf(msg, "%s (%.3d) %.24s: %s\n", name, pid, ti, fmt);
  43.  
  44. #ifdef DEBUG
  45. # ifdef LOGFILE
  46. #  undef LOGFILE
  47. # endif
  48. # define LOGFILE "u:/dev/tty"
  49. #endif
  50.  
  51.     if((log_fd = fopen(ux2dos(LOGFILE), "a")) == NULL)
  52.     {    fprintf(stderr,"%s: can't open log file\n",name);
  53.         perror(LOGFILE);
  54.         return;
  55.     }
  56.     if(vfprintf(log_fd,msg,ptr) == EOF)
  57.     {    fprintf(stderr,"%s: can't write to log file\n",name);
  58.         perror(LOGFILE);
  59.         fprintf(stderr,msg);
  60.     }
  61.     fclose(log_fd);
  62.  
  63.     free(msg);
  64.     va_end(ptr);
  65.  
  66. #endif /*LOGFILE */
  67. }
  68.  
  69. /* force a crontabs reread by sending a signal to CROND daemon */
  70.  
  71. void PokeDaemon(char cmd)
  72. {    int pipe;
  73.     
  74.     if((pipe=open(ux2dos(CRONPIPE),O_WRONLY)) >= 0)
  75.     {    if(write(pipe,&cmd,1) == 1)
  76.         {    close(pipe);
  77.             return;
  78.         }
  79.     }
  80.     close(pipe);
  81.     fputs(PROGNAME " : can't send signal to daemon\n",stderr);
  82.     return;
  83. }
  84.  
  85. void AlarmHandler(void)
  86. {    return;
  87. }
  88.  
  89. void cronsleep(unsigned long seconds)
  90. {
  91.     /* Avoid heavy system load while waiting for input :
  92.        Standard sleep functions in PURE C cause enormous load on a
  93.        multitasking system, as they are stupid loops !!
  94.     */
  95.     long mask=sigblock(0L);    /* get current mask */
  96.  
  97.     signal(SIGALRM,(__Sigfunc)AlarmHandler);
  98.  
  99. #ifdef DEBUG
  100.     printf("\nMasked signals: ");
  101.     PrintBin(31,sigblock(0L));
  102.     putchar('\n');
  103. #else
  104.     sigsetmask(mask & ~((1L<<SIGINT)|(1L<<SIGTERM)));
  105.     /* unmask blocked signals */
  106. #endif
  107.  
  108.     alarm((int)seconds);    /* set alarm time */
  109.     pause();                /* wait for alarm */
  110.  
  111.     sigsetmask(mask);    /* restore old mask when not sleeping */
  112. }
  113.